﻿using System;
using System.Linq;
using System.Web.Mvc;
using MVCLD.Helpers;
using MVCLD.Models;
using System.Web.Security;

namespace MVCLD.Controllers
{
    [Authorize(Roles="admin")]
    public class AdminController : ApplicationController
    {
        #region Data Access
        MVCLDServiceModels db = new MVCLDServiceModels();
        CategoryModels c = new CategoryModels();
        #endregion

        #region Article Related Admin
        // GET: /Admin/
        public ActionResult Index()
        {
            return View(db.GetLatestSubmittedUnconfirmedArticles().ToList());
        }


        // GET: /Admin/Details/etewrt-erwtwerter-ertwe
        public ActionResult Details(Guid id)
        {
            return View(db.GetSingleArticle(id));
        }

        public ActionResult Delete(Guid id)
        {
            if (db.DeleteSingleArticle(id))
            {
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Error");
            }
        
        }


        // GET: /Admin/Edit/5
        public ActionResult Edit(Guid id)
        {
            CategoryModels c = new CategoryModels();
            ViewData["CategoryDropDown"] = c.GetSubCategories(db.GetAllMainCategories().ToList());
            ViewData["ArticleTypes"] = db.GetAllArticleTypes().ToList();
            return View(db.GetSingleArticle(id));
        }


        // POST: /Admin/Edit/5
        [HttpPost]
        public ActionResult Edit(Guid id, FormCollection collection)
        {
            var TheArticle = db.GetSingleArticle(id);
            try
            {

                // TODO: Add update logic here
                UpdateModel(TheArticle);
                db.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                CategoryModels c = new CategoryModels();
                ViewData["CategoryDropDown"] = c.GetSubCategories(db.GetAllMainCategories().ToList());
                ViewData["ArticleTypes"] = db.GetAllArticleTypes().ToList();
                return View(TheArticle);
            }
        }

        public ActionResult LiveArticles(int? p) 
        {
            var articles = db.GetArticles(true);
            var paginatedarticles = new PaginatedList<Article>(articles, p ?? 0, 20);
            return View(paginatedarticles);
        }

        [HttpPost]
        public ActionResult LiveArticles(int? p, string searchstring)
        {
            if(String.IsNullOrEmpty(searchstring.SQLInject()))
            {
                return RedirectToAction("LiveArticles");
            }
            else
            {
                var articles = db.GetArticles(true).Where(s => s.ArticleTitle.Contains(searchstring.SQLInject()));
                var paginatedarticles = new PaginatedList<Article>(articles, p ?? 0, 20);
                return View(paginatedarticles);
            }
        }

        #endregion

        #region Category Related Admin
        // Show all Categories

        public ActionResult Categories(int? p)
        {
            var cats = db.GetAllCategories();
            var paginatedcats = new PaginatedList<Category>(cats, p ?? 0, 15);
            return View(paginatedcats);
        }

        [HttpPost]
        public ActionResult Categories(int? p, string searchstring)
        {
            if (String.IsNullOrEmpty(searchstring.SQLInject()))
            {
                return RedirectToAction("Categories");
            }
            else
            {
                var cats = db.GetAllCategories().Where(s => s.CategoryName.Contains(searchstring.SQLInject()));
                var paginatedcats = new PaginatedList<Category>(cats, p ?? 0, 15);
                return View(paginatedcats);   
            }
        }

        public ActionResult CreateCategory()
        {
            ViewData["CategoryDropDown"] = c.GetSubCategories(db.GetAllMainCategories().ToList());
            return View();
        }

        [HttpPost]
        public ActionResult CreateCategory(Category cat)
        {
            if (ModelState.IsValid)
            {
                // Create Category
                db.InsertCategory(cat);
                return RedirectToAction("Categories", "Admin");
            }
            else
            {
                ViewData["CategoryDropDown"] = c.GetSubCategories(db.GetAllMainCategories().ToList());
                return View();
            }
        }

        /// <summary>
        /// Delete a category
        /// </summary>
        /// <param name="ID">Category Guid ID</param>
        /// <returns>Category Index</returns>
        public ActionResult CategoryDelete(Guid ID) 
        {
            if (db.DeleteSingleCategory(ID))
            {
                return RedirectToAction("Categories", "Admin");
            }
            else
            {
                return RedirectToAction("Error", "Admin");
            }
        }

        public ActionResult CategoryEdit(Guid ID) 
        {
            ViewData["CategoryDropDown"] = c.GetSubCategories(db.GetAllMainCategories().ToList());
            return View(db.GetSingleCategory(ID));
        }

        [HttpPost]
        public ActionResult CategoryEdit(Guid ID, FormCollection collection)
        {

            var TheCat = db.GetSingleCategory(ID);
            try
            {
                // TODO: Add update logic here
                UpdateModel(TheCat);
                db.Save();
                return RedirectToAction("Categories");
            }
            catch
            {
                return RedirectToAction("Error");
            }
        }

        #endregion

        #region ArticleTypes
        public ActionResult ArticleTypes() 
        {
            return View(db.GetAllArticleTypes().ToList());
        }

        public ActionResult EditArticleType(Guid id) 
        {
            return View(db.GetSingleArticleType(id));
        }

        [HttpPost]
        public ActionResult EditArticleType(Guid id, FormCollection collection)
        {
            var ArtType = db.GetSingleArticleType(id);
            try
            {
                // TODO: Add update logic here
                UpdateModel(ArtType);
                db.Save();
                return RedirectToAction("ArticleTypes");
            }
            catch
            {
                return RedirectToAction("Error");
            }
        }

        public ActionResult DeleteArticleType(Guid id) 
        {
            if(db.DeleteSingleArticleType(id))
            {
                return RedirectToAction("ArticleTypes");
            }
            else
            {
                return RedirectToAction("Error");
            }
        }

        public ActionResult CreateArticleType() 
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateArticleType(ArticleType art)
        {
            if (ModelState.IsValid)
            {
                // Create Category
                db.InsertArticleType(art);
                return RedirectToAction("ArticleTypes");
            }
            else
            {
                return RedirectToAction("Error");
            }
        }

        #endregion

        #region General
        public ActionResult Error() 
        {
            return View();
        }

        [HttpPost]
        public ActionResult ChangePassword(FormCollection form) 
        {
            var OldPassword = Request.Form["OldPassword"];
            var NewPassword = Request.Form["NewPassword"];
            MembershipUser CurrentUser = Membership.GetUser();
            if (CurrentUser.ChangePassword(OldPassword, NewPassword))
            {
                return RedirectToAction("Index", "UserAdministration");
            }
            else
            {
                return RedirectToAction("Error");
            }
        }
        #endregion
            
    }
}
